package com.gzhryc.shared_device.oem.h5.mng;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.shared_device.common.payment.dao.models.CashWithdrawalConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStoreType;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class DataTools {

    public static void checkChildren(PlacePhysicalStoreType physicalStoreType, Map<String,List<PlacePhysicalStoreType>> data){
        List<PlacePhysicalStoreType> children = data.remove(physicalStoreType.getCode());
        if(children != null && children.size() > 0){
            physicalStoreType.setChildren(children);
            for(PlacePhysicalStoreType child : physicalStoreType.getChildren()){
                checkChildren(child,data);
            }
        }
    }

    public static CashWithdrawalConfig getCashWithdrawalConfig(HttpServletRequest request,String name) throws LogicException {
        CashWithdrawalConfig config = WebUtils.getParameter(request,name,CashWithdrawalConfig.class);
        if (config == null || config.getServiceCharge() == null || config.getServiceCharge() < 0 || config.getServiceCharge() >= 100) {
            throw new LogicException("请输入正确的代理提现手续费");
        }

        String minMoneyStr = WebUtils.getString(request, name + ".minMoneyStr");
        String maxMoneyStr = WebUtils.getString(request, name + ".maxMoneyStr");
        String autoMaxMoneyStr = WebUtils.getString(request, name + ".autoMaxMoneyStr");
        String dayUserMaxMoneyStr = WebUtils.getString(request, name + ".dayUserMaxMoneyStr");
        Double minMoney = null;
        Double maxMoney = null;
        Double autoMaxMoney = null;
        Double dayUserMaxMoney = null;
        if (minMoneyStr == null || maxMoneyStr == null || autoMaxMoneyStr == null) {
            throw new LogicException("金额不能为空");
        }
        if (StringTools.isNumber(minMoneyStr) && StringTools.isNumber(maxMoneyStr) && StringTools.isNumber(autoMaxMoneyStr)
                && StringTools.isNumber(dayUserMaxMoneyStr)) {
            minMoney = Double.valueOf(minMoneyStr);
            maxMoney = Double.valueOf(maxMoneyStr);
            autoMaxMoney = Double.valueOf(autoMaxMoneyStr);
            dayUserMaxMoney = Double.valueOf(dayUserMaxMoneyStr);
            if (minMoney <= 0 || maxMoney <= 0)
                throw new LogicException("请输入正确的提现金额");
            if (autoMaxMoney < 0)
                throw new LogicException("请输入正确的自动审核金额");
            if (dayUserMaxMoney < 0)
                throw new LogicException("请输入正确的代理日限额");
            if (minMoney > maxMoney)
                throw new LogicException("请输入正确的提现金额（最低-最高）");
        } else {
            throw new LogicException("请输入正确的提现金额");
        }

        if (config.getMonthStartDay() != null) {
            if (config.getMonthEndDay() != null) {
                if (config.getMonthEndDay() >= config.getMonthStartDay()) {
                    if (config.getMonthStartDay() >= 1 && config.getMonthStartDay() <= 31) {
                        if (config.getMonthEndDay() < 1 || config.getMonthEndDay() > 31) {
                            throw new LogicException("请输入每月1-31的某天");
                        }
                    } else {
                        throw new LogicException("请输入每月1-31的某天");
                    }
                }else {
                    throw new LogicException("输入起始天要小于结束天");
                }
            } else {
                throw new LogicException("请输入每月第几个天结束提现");
            }
        }

        config.setMinMoney(NumberTools.changeMoney(minMoney));
        config.setMaxMoney(NumberTools.changeMoney(maxMoney));
        config.setAutoMaxMoney(NumberTools.changeMoney(autoMaxMoney));
        config.setDayUserMaxMoney(NumberTools.changeMoney(dayUserMaxMoney));

        return config;
    }
}
